local Widget = require("widgets/widget")
local Image = require("widgets/image")
local Easing = require("easing")

local half_x, half_y = RESOLUTION_X / 2, RESOLUTION_Y / 2
local screen_width, screen_height = TheSim:GetScreenSize()
local function WorldPosToScreenPos(x, z)
    local map_x, map_y = TheWorld.minimap.MiniMap:WorldPosToMapPos(x, z, 0)
    local screen_x = ((map_x * half_x) + half_x) / RESOLUTION_X * screen_width
    local screen_y = ((map_y * half_y) + half_y) / RESOLUTION_Y * screen_height
    return screen_x, screen_y
end


local refreshflag
local AutoMapTiles = Class(Widget, function(self, mapscreen, setting)
    Widget._ctor(self, "AutoMapTiles")
    self.root = mapscreen:AddChild(Widget("root"))
    self.icons = {}

    local MapWidgetOnUpdate = mapscreen.OnUpdate
    self.iscave = TheWorld:HasTag("cave") and 1 or 0
    local typescreen = tostring(mapscreen)
    if typescreen == "MapScreen" then
        mapscreen.OnUpdate = function(mapscreen, ...)
            MapWidgetOnUpdate(mapscreen, ...)
            if setting.show then
                self.root:Show()
                local zoom = TheWorld.minimap.MiniMap:GetZoom()
                local scale = 0.85 - Easing.outExpo(zoom - 1, 0, 0.6, 8)
                for _, mapicon in pairs(self.icons) do
                    local x, y = WorldPosToScreenPos(mapicon.x, mapicon.z)
                    mapicon:SetPosition(x, y)
                    mapicon:SetScale(scale or 1)
                end
            else
                self.root:Hide()
            end
        end
    elseif typescreen == "HUD" then
        mapscreen.OnUpdate = function(mapscreen, ...)
            MapWidgetOnUpdate(mapscreen, ...)
            if TheCamera and TheCamera.mindistpitch == 90 and setting.show then
                self.root:Show()
                if not refreshflag then
                    self:Refresh()
                end
                for _, mapicon in pairs(self.icons) do
                    local x, y = TheSim:GetScreenPos(mapicon.x, 0, mapicon.z)
                    mapicon:SetPosition(x, y)
                end
            else
                self.root:Hide()
            end
        end
    end
    
    self:Refresh()
end)



function AutoMapTiles:AddMapIcon(id, pos_x, pos_y, altas, image)
    self.icons[id] = self.root:AddChild(Image(altas, image))
    self.icons[id].x = pos_x
    self.icons[id].z = pos_y
    self.icons[id]:SetScale(0.3)
end


local tmip_path = resolvefilepath("images/customicobyysh.xml")
local shke_path = resolvefilepath("images/shroomcake_moreicons.xml")
local minimap_path = "minimap/minimap_data.xml"
function AutoMapTiles:Refresh()
    local w_data = require("libs/worldmanager"):GetWorldDataMap("more_map_tiles")
    local id_icons = {}
    -- 遍历最新数据，将没有的图标增加进显示
    for id, data in pairs(w_data)do
        if data.c and data.x and data.z and data.c == self.iscave then
            table.insert(id_icons, id)
            if not self.icons[id] then
                local info = data.a and data.i and data
                if not info then
                    if data.p then
                        local image = data.p..".tex"
                        local png = data.p..".png"
                        local inv_altas = GetInventoryItemAtlas(image)
                        if TheSim:AtlasContains(tmip_path, image) then
                            info = { a= tmip_path, i = image }
                        elseif TheSim:AtlasContains(minimap_path, png) then
                            info = { a= minimap_path, i = png }
                        elseif TheSim:AtlasContains(inv_altas, image)  then
                            info = { a= inv_altas, i = image }
                        elseif TheSim:AtlasContains(shke_path, image) then
                            info = { a= shke_path, i = image }
                        end
                    end
                end
                if info then
                    -- print(id, data.x, data.z, info.a, info.i)
                    self:AddMapIcon(id, data.x, data.z, info.a, info.i)
                end
            end
        end
    end
    -- 遍历显示，将不在最新数据中的图标都删除
    -- 其实没有必要，因为扫图功能只会运行一次，但是测试阶段仍然需要这个
    for id,_ in pairs(self.icons)do
        if not table.contains(id_icons, id) then
            self.icons[id]:Kill()
            self.icons[id] = nil
        end
    end
end


return AutoMapTiles
